
$(document).ready(function(){
//	alert('Yoo Hoo~!');

	$("#dialog_faculty_desc").dialog({
		bgiframe: true,
		autoOpen: false,
		width: 500,
		modal: true,
		buttons: {
			'Save': function() {
//				schooldesc.removeClass('ui-state-error');
				if ($('#facultydesc').val().length >0 ) {
					temp = $('#facultydesc').val();
					temp = temp.replace(/\n/g,"<br>");
					$.post("json.php",{"task": "saveFacultyDesc",
					"id":$('#facultyid').val(),"description": temp,
					"name": $('#newfacultyname').val() } , 
						function(data, textStatus){
							if(data == "1") {
//								$('#spnFacultyDesc').empty();
								$('#spnFacultyDesc').html(temp);
								$('#facultyname').text($('#newfacultyname').val());
								alert ('Save success. ');
								$("#dialog_faculty_desc").dialog('close');
							} else {
								alert ('Save error: ' + data);
							}
						}
					);
				} 
				
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
//			schooldesc.val('').removeClass('ui-state-error');
		}
	});

	$("#dialog_faculty_pic").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: true,
		buttons: {
			'Save': function() {
				$.post("json.php",{"task": "saveFacultyPic",
				"id":$('#selChairperson').val(),"facultyid": $('#facultyid').val() } , 
					function(data, textStatus){
						if(data == "1") {
							$('#spnChairperson').text($('#selChairperson').children(':selected').text());
							alert ('Save success. ');
						} else {
							alert ('Save error: ' + data);
						}
					}
				);
				$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {

		}
	});

	$("#dialog_newnotice").dialog({
		bgiframe: true,
		autoOpen: false,
		width: 490,
		modal: true,
		buttons: {
			'Save': function() {
				temp = $('#new_notice_content').val();
				temp = temp.replace(/\n/g,"<br>");
				$.getJSON("json.php", { "task": "addNewNotice",
				"pageid":"2", "subject": $('#new_notice_subject').val(),
				"content": temp, "schoolid": $('#schoolid').val(), "referenceid": $('#facultyid').val() } , 
					function(data){
						$('#noticelist').prepend('<span id="spnnotice' + data.id + '"><b>' + data.subject + 
						'</b><br/><span>' +
						data.content + '</span> ' +
						WriteActionButton("Edit", "editNotice(" + data.id + ")") + ' ' +
						WriteActionButton("Delete", "deleteNotice(" + data.id + ")") +
						'<hr/></span>' );
//							alert('Save success. ');
						$("#dialog_newnotice").dialog('close');
					}
				);
//					$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$('#new_notice_subject').val('');
			$('#new_notice_content').val('');
		}
	});

	$("#dialog_editnotice").dialog({
		bgiframe: true,
		autoOpen: false,
		width: 490,
		modal: true,
		buttons: {
			'Save': function() {
				temp = $('#edit_notice_content').val();
				temp = temp.replace(/\n/g,"<br>");
				$.getJSON("json.php", { "task": "saveNewNotice", "id": $('#edit_notice_id').val(),
				"pageid": 2, "subject": $('#edit_notice_subject').val(),
				"content": temp, "schoolid": $('#schoolid').val() } , 
					function(data){
//							spanname = '#spnnotice' + data.id;
						$('#spnnotice' + data.id).children('span').html(data.content);
						$('#spnnotice' + data.id).children('b').html(data.subject);
						alert('Save success. ');
						$("#dialog_editnotice").dialog('close');
					}
				);
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$('#edit_notice_subject').val('');
			$('#edit_notice_content').val('');
		}
	});

	$("#dialog_new_department").dialog({
		bgiframe: true,
		autoOpen: false,
		width: 490,
		modal: true,
		buttons: {
			'Save': function() {
				temp = $('#department_description').val();
				temp = temp.replace(/\n/g,"<br>");
				$.post("json.php", { "task": "addNewDepartment",
				"name": $('#department_name').val(), "facultyid": $('#facultyid').val(),
				"description": temp, "pic": $('#dean').val() } , 
					function(data){
						if(data > 0 ){
							$('#departmentlist').append('<li id="lidepartment'+data+'">' +
								 $('#department_name').val() +
								 ' ' +
								WriteActionButton("View", "viewDepartment('" + data + "')" ) +
								' ' +
								WriteActionButton("Delete", "deleteDepartment('" + data + "')") +
								'</li>' );
						} else {
							alert("Server error: " + data);
						}
						$("#dialog_new_department").dialog('close');
					}
				);
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$('#department_name').val('');
			$('#department_description').val('');
		}
	});

// -------------------------------------------------------------------------------------

	$('a.ui-corner-all').hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	).mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});

	$('#editFacultyDesc').click(function(){
		x = $('#spnFacultyDesc').html();
		$('#facultydesc').val(x.replace(/<br>/g,"\n").replace(/<br\/>/g,"\n"));
		$('#newfacultyname').val($('#facultyname').text());
		$('#dialog_faculty_desc').dialog('open');
	});
	
	$('#editFacultyPic').click(function() {
		$('#selChairperson').empty();
		$.getJSON("json.php",{task: "getPresidentCandidates", id: $('#schoolid').val()},
			function(data){
				$.each(data.candidates, function(i,item){ 
		            $('#selChairperson').append('<option value="' + item.id + '">' + item.name + '</option>');
      			}); 
				$('#dialog_faculty_pic').dialog('open');
			}
		);
	});

	$('#addFacultyNotice').click(function() {
		$('#dialog_newnotice').dialog('open');
	});

	$('#addDepartment').click(function() {
		$('#dean').empty();
		$.getJSON("json.php",{
			"task": "getPresidentCandidates",
			"id": $('#schoolid').val()
		}, function(data){
			$.each(data.candidates, function(i,item){ 
	            $('#dean').append('<option value="' + item.id + '">' + item.name + '</option>');
   			}); 
		});
		$('#dialog_new_department').dialog('open');
	});
	
	$('#linkHome').click(function(){
		$('#hiddentask').val('default');
		$('#hiddenid').val(1);
		$('#hiddenform').submit();

	});

});

function editNotice(nid){
	// load data from json
//	alert('Editing notice '+ id);
		temp = $('#spnnotice'+nid).children('span').html();
		temp = temp.replace(/<br>/g, "\n");
		$('#edit_notice_subject').val($('#spnnotice'+nid).children('b').html());
		$('#edit_notice_content').val(temp);
		$('#edit_notice_id').val(nid);
		// open edit dialog
		$('#dialog_editnotice').dialog('open');
}

function deleteNotice(noticeid){
	// ask user if want to delete
	var answer = confirm("Do you really want to delete this notice?");
	if(answer == true){
	// perform ajax  request
		$.post("json.php",{task:"deleteNotice",id:noticeid},
		function(data){
			if(data=="1"){
				alert("Notice deleted.");
				$('#spnnotice'+noticeid).remove();
			}
		});			
	}
	// show success.
}

function deleteDepartment(facid){
	var answer = confirm("Do you really want to delete this department?");
	if(answer == true){
	// perform ajax  request
		$.post("json.php",{task:"deleteDepartment",id:facid},
		function(data){
			if(data=="1"){
				alert("Department deleted.");
				$('#lifaculty'+facid).remove();
			}
		});			
	}
}

function viewDepartment(fid) {
	$('#hiddentask').val('department');
	$('#hiddenid').val(fid);
	$('#hiddenform').submit();
}

